package com.zhangxin.aiInterview.manager.disruptor.strategy;

import com.zhangxin.aiInterview.manager.disruptor.WsEvent;
import com.zhangxin.aiInterview.manager.xfapi.websocket.ttsmodel.TtsWsManager;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.java_websocket.client.WebSocketClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * ws发送合成文本转语音消息策略
 */
@Slf4j
//@Component
@Data
@Deprecated
public class TtsWsSendMsg implements WsSendMsg {
    @Value("${xunfei.appid}")
    public String APPID;
    // 合成文本编码格式
    @Value("${xunfei.tts.TTE}")
    public String TTE; // 小语种必须使用UNICODE编码作为值

    // 发音人参数。到控制台-我的应用-语音合成-添加试用或购买发音人，添加后即显示该发音人参数值，若试用未添加的发音人会报错11200
    @Value("${xunfei.tts.VCN}")
    public String VCN;

    @Override
    public void sendMessage(WsEvent wsEvent) {
        //获取ai提问文本
        String txt = wsEvent.getTxt().trim();
        //获取tts的ws连接
        WebSocketClient ttsWsClient = TtsWsManager.getConnection(wsEvent.getUserId());

        String requestJson;
        requestJson = "{\n" +
                "  \"common\": {\n" +
                "    \"app_id\": \"" + APPID + "\"\n" +
                "  },\n" +
                "  \"business\": {\n" +
                "    \"aue\": \"lame\",\n" +
                "    \"tte\": \"" + TTE + "\",\n" +
                "    \"ent\": \"intp65\",\n" +
                "    \"vcn\": \"" + VCN + "\",\n" +
                "    \"pitch\": 50,\n" +
                "    \"speed\": 50\n" +
                "  },\n" +
                "  \"data\": {\n" +
                "    \"status\": 2,\n" +
                "    \"text\": \"" + Base64.getEncoder().encodeToString(txt.getBytes(StandardCharsets.UTF_8)) + "\"\n" +
                "  }\n" +
                "}";
        ttsWsClient.send(requestJson);
    }
}